package com.gitee.huanminabc.utils_common.file;

import lombok.SneakyThrows;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Description TODO
 * @Author huanmin
 * @Date 2024/4/26 上午10:27
 */
public class ImageUtil {


    //将网络url转换为byte[]
    @SneakyThrows
    public static byte[] urlToBytes(String imageUrl) {
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();
        InputStream is = connection.getInputStream();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len;
        while ((len = is.read(buffer)) != -1) {
            baos.write(buffer, 0, len);
        }
        is.close();
        baos.flush();
        byte[] byteArray = baos.toByteArray();
        baos.close();
        return byteArray;
    }

    //获取网络图片的宽高
    public static int[] getImageSizeFromUrl(String imageUrl) throws Exception {
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();
        InputStream is = connection.getInputStream();
        BufferedImage image = ImageIO.read(is);
        return new int[]{image.getWidth(), image.getHeight()};
    }
    //获取网络图片文件大小
    @SneakyThrows
    public static long getImageFileSizeFromUrl(String imageUrl){
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("HEAD"); // 发送HEAD请求而不是GET请求
        connection.connect();

        // 获取响应头中的Content-Length字段
        String contentLengthHeader = connection.getHeaderField("Content-Length");

        // 将Content-Length转换为长整型
        long fileSize = -1; // 默认值，表示未知大小
        if (contentLengthHeader != null) {
            fileSize = Long.parseLong(contentLengthHeader);
        }

        connection.disconnect(); // 关闭连接
        return fileSize;
    }

    //将网络url转换为BufferedImage
    public static BufferedImage urlToBufferedImage(String imageUrl) throws Exception {
        URL url = new URL(imageUrl);
        URLConnection connection = url.openConnection();
        InputStream is = connection.getInputStream();
        return ImageIO.read(is);
    }
    //将本地文件转换为BufferedImage
    public static BufferedImage fileToBufferedImage(String filePath) throws Exception {
        return ImageIO.read(new URL(filePath));
    }
    /**
     * BufferedImage转换为byte[]
     * @param image BufferedImage
     * @param format 图片格式 jpg ,png, gif
     * @return
     * @throws Exception
     */
    public static byte[] bufferedImageToBytes(BufferedImage image, String format) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, format, baos);
        baos.flush();
        byte[] byteArray = baos.toByteArray();
        baos.close();
        return byteArray;
    }


    //图像压缩
    public static BufferedImage compressImage(BufferedImage image, int targetWidth, int targetHeight) {
        BufferedImage result = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        result.getGraphics().drawImage(image, 0, 0, targetWidth, targetHeight, null);
        return result;
    }

    //图像略缩图, 给定宽高, 截取中间部分
    public static BufferedImage thumbnailImage(BufferedImage image, int targetWidth, int targetHeight) {
        int width = image.getWidth();
        int height = image.getHeight();
        if (width < targetWidth && height < targetHeight) {
            return image;
        }
        int x = 0, y = 0;
        if (width > targetWidth) {
            x = (width - targetWidth) / 2;
        }
        if (height > targetHeight) {
            y = (height - targetHeight) / 2;
        }
        return image.getSubimage(x, y, targetWidth, targetHeight);
    }

}
